package com.rambo.baselib.fraction;

import com.rambo.baselib.ResourceTable;
import com.rambo.baselib.msgbus.EventBus;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.mvp.IView;
import com.rambo.baselib.util.Loading;
import com.rambo.baselib.util.LogUtil;
import com.rambo.baselib.util.ToastUtil;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;

import java.io.Serializable;


/***********************************************************************
 * <p>@description: 碎片窗口 类似 Android fragment
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/13 16:07
 **********************************************************************/
public abstract class BaseFraction<V extends IView, P extends BasePresenter<V>> extends Fraction implements IView {
    protected String TAG = getClass().getSimpleName();
    protected P presenter;
    protected Loading loading;
    protected Context context;
    private CommonEventSubscriber commonEventSubscriber;
    private MatchingSkills matchingSkills;
//    public BaseFraction() {
//    }

    public BaseFraction(Context context) {
        this.context = context;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = super.onComponentAttached(scatter, container, intent);
        if (component == null) {
            StackLayout stackLayout = (StackLayout) scatter.parse(ResourceTable.Layout_fraction_base, container, false);
            Component contentComponent = scatter.parse(getLayoutResId(), null, false);
            StackLayout.LayoutConfig loadingConfig = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT,
                    StackLayout.LayoutConfig.MATCH_PARENT);
            stackLayout.addComponent(contentComponent, loadingConfig);
            loading = new Loading(this);
            loading.init(contentComponent);
            loading.setItemClickedListener(new ListContainer.ItemClickedListener() {
                @Override
                public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                    ToastUtil.toast("哈哈，逗你玩呢！！！还没实现重新加载逻辑");
                }
            });
            stackLayout.addComponent(loading, new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_PARENT));
            component = stackLayout;
        }
        LogUtil.d(TAG + "==============onComponentAttached");
        subscriberEvent();
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attach((V) this);
        }
        initView(component);
        registerClick();
        fillData();
        return component;
    }

    private void subscriberEvent() {
        matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(EventBus.EVENT_ACTION);
        commonEventSubscriber = new CommonEventSubscriber(new CommonEventSubscribeInfo(matchingSkills)) {
            @Override
            public void onReceiveEvent(CommonEventData commonEventData) {
                LogUtil.e("======onReceiveEvent====" + Thread.currentThread().getName());
                int type = commonEventData.getIntent().getIntParam(EventBus.TYPE_KEY, -1);
                Serializable serializable = commonEventData.getIntent().getSerializableParam(EventBus.DATA_KEY);
                onEventReceive(type, serializable);
            }
        };
        EventBus.getEventBus().subscriber(commonEventSubscriber);
    }

    /**
     * 订阅接收的信息
     *
     * @param eventBean
     */
    protected void onEventReceive(int type, Object eventBean) {
        LogUtil.e("======onEventReceive====" + eventBean!=null?eventBean.toString():"");
    }


    protected abstract int getLayoutResId();

    protected abstract P createPresenter();

    /**
     * 初始化视图控件
     *
     * @param component
     */
    protected void initView(Component component) {

    }

    /**
     * 注册点击事件
     */
    protected void registerClick() {

    }

    /**
     * 填充数据
     */
    protected void fillData() {

    }

    @Override
    protected void onComponentDetach() {
        if (presenter != null) {
            presenter.detach();
        }
        super.onComponentDetach();
    }


    @Override
    public void showLoading() {
        showLoading(null, false);
    }

    @Override
    public void showLoading(String msg, boolean isDialogStyle) {
        if (isDialogStyle) {
            return;
        }
        loading.updateLoadState(Loading.LoadingState.LOADING);
    }

    @Override
    public void hideLoading() {
        loading.updateLoadState(Loading.LoadingState.SUCCESS);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getEventBus().unSubscriber(commonEventSubscriber);
    }
}
